from django.shortcuts import render, get_object_or_404, redirect
from bl.models import Blog
from utils.json_to_dict import json_to_dict
from .models import Comment
from .forms import CommentForm
from django.http import JsonResponse, HttpResponse
from django.views.decorators.http import require_POST, require_http_methods


@require_http_methods(['GET', 'POST'])
def sub_comment(request):
    name = request.POST.get('name')
    email = request.POST.get('email')
    print(11, name, 22, email)
    url = request.POST.get('url')
    text = request.POST.get('text')
    blog_id = request.POST.get('blog_id')
    print('blog_id', blog_id)
    try:
        blog = Blog.objects.get_one_object(id=blog_id)
        comments = blog.comment_set.all()
        print(111111, blog, comments)
    except Exception as e:
        print(str(e))
    comment = Comment.objects.add_comment(name, email, url, text, blog.id)

    return render(request, 'bl/xiangqing.html', locals())
    # return render(request, 'bl/liuyan.html', {'comment': comment})

def comment_list(request):
    # data = json_to_dict(request)
    # id = data['id']
    id = request.POST.get('blog_id')
    print('id',id)
    blog = Blog.objects.get_one_object(id)
    comments = blog.comment_set.all()
    print(66, comments)
    return JsonResponse({'res':1})

    return render(request, 'bl/xiangqing.html', locals())













